package com.buba.goods.controller;

import com.buba.goods.pojo.ItemsType;
import com.buba.goods.pojo.MatterInfo;
import com.buba.goods.pojo.MatterPrice;
import com.buba.goods.service.MatterService;
import com.buba.system.pojo.Page;
import com.buba.util.GenerateKey;
import com.buba.util.MyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RequestMapping("/Matter")
@RestController
public class MatterController {

    private MatterService matterService;

    @RequestMapping("/addMatterPrice")
    public Map<String,Object> addMatterPrice(@RequestParam Map<String, Object> map){
        map.put("TimeNo", GenerateKey.getKey(""));
        Integer count=matterService.addMatterPrice(map);
        if(count==1){
            map.put("message","添加成功");
        }else{
            map.put("message","添加失败");
        }
        return map;
    }

    /**
     * 查询货物种类
     * @return
     */
    @RequestMapping("/selItemsType")
    public Map<String,Object> selItemsType(){
        Map<String,Object> map=new HashMap<>();
        List<ItemsType> list=matterService.selItemsType();
        map.put("list",list);
        return map;
    }
    @RequestMapping("/selMatterPrice")
    public Page selMatterPrice(@RequestParam Map<String, Object> map,String itemsName){
        Page page = new Page((Integer.parseInt(map.get("pageNow").toString())),(Integer.parseInt(map.get("pageCount").toString())));
        map.put("page", page);
        map.put("itemsName",itemsName);
        //查询条数
        page.computePageTotal(matterService.selMatterPriceCount(map));
        //查询信息
        List<MatterPrice> list = matterService.selMatterPrice(map);
        //设置信息
        page.setMatterPricelist(list);
        return page;
    }
    @RequestMapping("/delMatterPriceAll")
    private Map<String,Object> delMatterPriceAll(String ids){
        Map<String,Object> map=new HashMap<>();
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        Integer count=matterService.delMatterPriceAll(list);
        System.out.println(count);
        if(count==null){
            map.put("message","删除失败");

        }else{
            map.put("message","删除"+count+"条数据");
        }
        return map;
    }
    @RequestMapping("/delMatterInfoAll")
    private Map<String,Object> delMatterInfoAll(String ids){
        Map<String,Object> map=new HashMap<>();
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        Integer count=matterService.delMatterInfoAll(list);
        if(count!=null){
            map.put("message","删除"+count+"条数据");
        }else{
            map.put("message","删除失败");
        }
        return map;
    }
    @RequestMapping("/addMatterInfo")
    public Map<String,Object> addMatterInfo(@RequestParam Map<String, Object> map){
        map.put("TimeNo", GenerateKey.getKey(""));
        Integer count=matterService.addMatterInfo(map);
        if(count==1){
            map.put("message","添加成功");
        }else{
            map.put("message","添加失败");
        }
        return map;
    }
    @RequestMapping("/selMatterInfoItemsInfoNo")
    public Map<String,Object> selMatterInfoItemsInfoNo(){
        Map<String,Object> map = new HashMap<>();
        List<MatterInfo> list=matterService.selMatterInfoItemsInfoNo();
        map.put("list",list);
        return map;
    }

    /**
     * 饼状图查询数据
     * @return
     */
    @RequestMapping("/selMatterInfoItemsInfoNo1")
    public Map<String,Object> selMatterInfoItemsInfoNo1(){
        Map<String,Object> map = new HashMap<>();
        //分组查询数据不重复
        List<MatterInfo> list=matterService.selMatterInfoItemsInfoNo1();
        for(MatterInfo m:list){
            String itemsTypeName=matterService.selItemsTypeById(m.getItemsTypeId());
            m.setItemsTypeName(itemsTypeName);
            //根据ID查询求和函数并赋值
            Integer itemsCounts=matterService.selMatterInfoItemsInfoNo2(m.getItemsTypeId());
            m.setItemsCounts(itemsCounts);
        }
        //数据进行排序
        Collections.sort(list);
        map.put("list",list);
        return map;
    }
    @RequestMapping("/selMatterPriceItemsNo")
    public Map<String,Object> selMatterPriceItemsNo(){
        Map<String,Object> map = new HashMap<>();
        List<MatterPrice> list=matterService.selMatterPriceByExcel();
        map.put("list",list);
        return map;
    }
    @RequestMapping("/selMatterInfo")
    public Page selMatterInfo(@RequestParam Map<String, Object> map,String itemsName){
        Page page = new Page((Integer.parseInt(map.get("pageNow").toString())),(Integer.parseInt(map.get("pageCount").toString())));
        map.put("page", page);
        map.put("itemsName",itemsName);
        //查询条数
        page.computePageTotal(matterService.selMatterInfoCount(map));
        //查询信息
        List<MatterInfo> list = matterService.selMatterInfo(map);
        //设置信息
        for(MatterInfo m:list){
            String itemsTypeName=matterService.selItemsTypeById(m.getItemsTypeId());
            m.setItemsTypeName(itemsTypeName);
        }
        page.setMatterInfoList(list);
        return page;
    }
    @RequestMapping("/updMatterInfo")
    public Map<String,Object> updMatterInfo(@RequestParam Map<String, Object> map){
        Integer count=matterService.updMatterInfo(map);
        if(count==1){
            map.put("message","修改成功");
        }else{
            map.put("message","修改失败");
        }
        return map;
    }
    @RequestMapping("/updMatterPrice")
    public Map<String,Object> updMatterPrice(@RequestParam Map<String, Object> map){
        Integer count=matterService.updMatterPrice(map);
        if(count==1){
            map.put("message","修改成功");
        }else{
            map.put("message","修改失败");
        }
        return map;
    }
    //导出到excel
    @RequestMapping("/importCustomerExcel")
    public void importCustomerExcel(HttpServletResponse response) throws Exception {
        Map<String,Object> map=new HashMap<>();
        List<MatterPrice> list=matterService.selMatterPriceByExcel();
        MyExcel<MatterPrice> myExcel= new MyExcel<>();
        myExcel.exportExcel("物质价格表",list, MatterPrice.class,response);
    }
    @RequestMapping("/importExcel")
    public Boolean importCustomerExcel(@RequestParam("file") MultipartFile upFile) throws Exception {
        List<MatterPrice> list = new MyExcel<MatterPrice>().importExcel(upFile, MatterPrice.class);
        try{
            for (MatterPrice l: list) {
                matterService.addMatterPrice2(l);
            }
        }catch (Exception e) {
            throw new Exception("批量录入用户失败，请检查表格中的数据是否和数据库中的数据冲突！");

        }
        return true;
    }

    @Autowired
    @Qualifier("matterServiceImpl")
    public void setMatterService(MatterService matterService) {
        this.matterService = matterService;
    }
}
